/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Authentification.java
 *
 * Created on 5 janv. 2009, 00:15:41
 */

package fr.miage.shopping.presentation;

import fr.miage.shopping.metier.entite.Admin;
import fr.miage.shopping.metier.entite.Client;
import fr.miage.shopping.metier.impl.AuthentificationBusinessServiceImpl;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

/**
 *
 * @author romain
 */
public class Authentification extends javax.swing.JFrame {

    /** Creates new form Authentification */
    public Authentification() {
        initComponents();
         this.setLocationRelativeTo(this.getParent());
        
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelAuth = new javax.swing.JPanel();
        lMail = new javax.swing.JLabel();
        tMail = new javax.swing.JTextField();
        lPass = new javax.swing.JLabel();
        bValider = new javax.swing.JButton();
        bInscription = new javax.swing.JButton();
        tPass = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Authentification");
        setBackground(new java.awt.Color(51, 102, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImage(getIconImage());

        panelAuth.setBackground(new java.awt.Color(51, 51, 255));

        lMail.setText("Email :");

        lPass.setText("Mot de passe : ");

        bValider.setText("Valider");
        bValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bValiderActionPerformed(evt);
            }
        });

        bInscription.setText("S'inscrire");
        bInscription.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bInscriptionActionPerformed(evt);
            }
        });

        tPass.setText("jPasswordField1");

        javax.swing.GroupLayout panelAuthLayout = new javax.swing.GroupLayout(panelAuth);
        panelAuth.setLayout(panelAuthLayout);
        panelAuthLayout.setHorizontalGroup(
            panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAuthLayout.createSequentialGroup()
                .addContainerGap(59, Short.MAX_VALUE)
                .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bValider)
                    .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAuthLayout.createSequentialGroup()
                            .addComponent(lMail)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED))
                        .addGroup(panelAuthLayout.createSequentialGroup()
                            .addComponent(lPass)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))))
                .addGap(19, 19, 19)
                .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAuthLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(bInscription)
                        .addGap(29, 29, 29))
                    .addGroup(panelAuthLayout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tPass, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(tMail, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(49, 49, 49))
        );
        panelAuthLayout.setVerticalGroup(
            panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelAuthLayout.createSequentialGroup()
                .addContainerGap(24, Short.MAX_VALUE)
                .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lMail)
                    .addComponent(tMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lPass)
                    .addComponent(tPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(panelAuthLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bValider)
                    .addComponent(bInscription))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelAuth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelAuth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Action du bouton valider
     * @param evt
     */
    private void bValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bValiderActionPerformed
       AuthentificationBusinessServiceImpl bs = new AuthentificationBusinessServiceImpl();
       Admin admin = null;
       Client client = null;
       if((this.tMail.getText().indexOf("@"))==-1){
           admin = bs.authentifierAdmin(tMail.getText(), tPass.getText());
       }
       else {
           client = bs.authentifierClient(tMail.getText(), tPass.getText());
       }
       if ((admin==null)&&(client==null)){
            JOptionPane.showMessageDialog(null,"L'authentification a échoué, veuillez recommencer","Echec authentification",JOptionPane.WARNING_MESSAGE);
       }
       else {
           MonAccueil accueil = new MonAccueil(admin,client);
           if (admin!=null){
               accueil.setAdmin(admin);
               accueil.setTitle(admin.getLogin()+ " "+admin.getRole().getRole());
           }
           else if (client!=null){
               accueil.setClient(client);
               accueil.setTitle(client.getNom()+" "+client.getPrenom());
           }
           accueil.setVisible(true);
            this.setVisible(false);
       }
    }//GEN-LAST:event_bValiderActionPerformed

    /**
     * action du bouton inscription
     * @param evt
     */
    private void bInscriptionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bInscriptionActionPerformed
       this.setVisible(false);
        new Inscription().setVisible(true);
    }//GEN-LAST:event_bInscriptionActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bInscription;
    private javax.swing.JButton bValider;
    private javax.swing.JLabel lMail;
    private javax.swing.JLabel lPass;
    private javax.swing.JPanel panelAuth;
    private javax.swing.JTextField tMail;
    private javax.swing.JPasswordField tPass;
    // End of variables declaration//GEN-END:variables

    public void setTMail(JTextField tMail) {
        this.tMail = tMail;
    }

    public void setTPass(JPasswordField tPass) {
        this.tPass = tPass;
    }

    /**
     * test le formulaire
     * @return
     */
    private boolean erreurForm() {
        boolean noError = true;

        if ((this.tMail == null) || (this.tMail.getText().length()<5) ||
                ((this.tMail.getText().indexOf("@")==-1))){
            this.tMail.setBackground(Color.RED);
            JOptionPane.showMessageDialog(null,"Le champ Email est incorrect. Veuillez saisir un email valide !","Email invalide",JOptionPane.WARNING_MESSAGE);
            noError = false;
        }
        else {
             this.tMail.setBackground(Color.WHITE);
        }

        if ((this.tPass == null) || (this.tPass.getText().length()<1)){
            this.tPass.setBackground(Color.RED);
            JOptionPane.showMessageDialog(null,"Le champ Mot de passe est incorrect. Veuillez saisir un mot de passe !","Mot de passe invalide",JOptionPane.WARNING_MESSAGE);
            noError = false;
        }

        return noError;
    }

}
